package com.shd.edanpei.view.picker;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import com.shd.edanpei.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 列表选择器
 */
public class List4ItemPicker {

    /**
     * 定义结果回调接口
     */
    public interface ResultHandler {
        void handle(String str);
    }

    public enum SCROLL_TYPE {
        HOUR(1),
        MINUTE(2);

        SCROLL_TYPE(int value) {
            this.value = value;
        }

        public int value;
    }

    private int scrollUnits = SCROLL_TYPE.HOUR.value + SCROLL_TYPE.MINUTE.value;
    private ResultHandler handler;
    private Context context;
    private boolean canAccess = false;

    private Dialog datePickerDialog;
    private PickerView picker_b_one, picker_b_two, picker_b_three, picker_b_four;

    private static final int MAX_MINUTE = 59;
    private static final int MAX_HOUR = 23;
    private static final int MIN_MINUTE = 0;
    private static final int MIN_HOUR = 0;
    private static final int MAX_MONTH = 12;

    private String title;
    private TextView tv_cancle, tv_title, tv_select, tv_unit;
    private List<String> list;
    private String mOne, mTwo, mThree, mFour;
    private String mUnit;

    public List4ItemPicker(Context context, String title, String unit, ResultHandler resultHandler) {
        canAccess = true;
        this.context = context;
        this.handler = resultHandler;
        this.title = title;
        this.mUnit = unit;
        list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i + "");
        }
        initDialog();
        initView();
    }

    private void initDialog() {
        if (datePickerDialog == null) {
            datePickerDialog = new Dialog(context, R.style.time_dialog);
            datePickerDialog.setCancelable(false);
            datePickerDialog.setCanceledOnTouchOutside(true);
            datePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            datePickerDialog.setContentView(R.layout.picker_list_4item);
            Window window = datePickerDialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = dm.widthPixels;
            window.setAttributes(lp);
        }
    }

    private void initView() {
        picker_b_one = datePickerDialog.findViewById(R.id.picker_b_one);
        picker_b_two = datePickerDialog.findViewById(R.id.picker_b_two);
        picker_b_three = datePickerDialog.findViewById(R.id.picker_b_three);
        picker_b_four = datePickerDialog.findViewById(R.id.picker_b_four);
        tv_unit = datePickerDialog.findViewById(R.id.tv_unit);

        tv_title = datePickerDialog.findViewById(R.id.tv_title);
        tv_select = datePickerDialog.findViewById(R.id.tv_select);
        tv_cancle = datePickerDialog.findViewById(R.id.tv_cancle);

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                datePickerDialog.dismiss();
            }
        });

        tv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handler.handle(mOne + mTwo + mThree + mFour);
                datePickerDialog.dismiss();
            }
        });
    }

    private void initTimer() {


    }


    private void loadComponent() {
        picker_b_one.setData(list);
        picker_b_two.setData(list);
        picker_b_three.setData(list);
        picker_b_four.setData(list);
        tv_unit.setText(mUnit);
        picker_b_one.setIsLoop(false);
        picker_b_two.setIsLoop(false);
        picker_b_three.setIsLoop(false);
        picker_b_four.setIsLoop(false);
        mOne = list.get(0);
        mTwo = list.get(0);
        mThree = list.get(0);
        mFour = list.get(0);
        tv_title.setText(title);
        picker_b_one.setSelected(0);
        picker_b_two.setSelected(0);
        picker_b_three.setSelected(0);
        picker_b_four.setSelected(0);
        executeScroll();
    }

    private void addListener() {
        picker_b_one.setOnSelectListener(new PickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                mOne = text;
            }
        });
        picker_b_two.setOnSelectListener(new PickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                mTwo = text;
            }
        });
        picker_b_three.setOnSelectListener(new PickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                mThree = text;
            }
        });
        picker_b_four.setOnSelectListener(new PickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                mFour = text;
            }
        });

    }

    private void executeScroll() {
        picker_b_one.setCanScroll(list.size() > 1);
        picker_b_two.setCanScroll(list.size() > 1);
        picker_b_three.setCanScroll(list.size() > 1);
        picker_b_four.setCanScroll(list.size() > 1);
    }

    public void show() {
        if (canAccess) {
            canAccess = true;
            loadComponent();
            addListener();
//            setSelectedTime(time);
            datePickerDialog.show();
        }
    }

    /**
     * 设置日期控件默认选中的时间
     */
    public void setSelectedTime(String time) {

    }

}
